<!--景区-->
<template>
  <div class="user-list">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="用户名字">
        <el-input v-model="formInline.userName" placeholder="名字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getProfile(1)">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="line"></div>
    <el-table
      :data="users"
      style="width: 100%">
      <el-table-column
        label="创建时间"
        width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createdTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用户名称"
        width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.userName }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column
        label="账号"
        width="200">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.userAccount }}</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="用户类型"
        width="180">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.userType }}</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="pageInfo.total>0"
      :current-page="pageInfo.page_index"
      @current-change="getProfile"
      :total="pageInfo.total"
      :page-size="pageInfo.page_size"
      class="list-page"
      background
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>
<script>
export default {
  name: 'scenicSpotList',
  components: {},
  data () {
    return {
      formInline: {
        userName: ''
      },
      pageInfo: {
        page_index: 1, // 当前位于哪页
        total: 0, // 总数
        page_size: 15, // 1页显示多少条
        page_sizes: [5, 10, 15, 20], // 每页显示多少条
        layout: 'total, sizes, prev, pager, next, jumper' // 翻页属性
      },
      users: [{
        id: '',
        createdTime: '',
        userName: '',
        userAccount: '',
        userType: ''
      }]
    }
  },
  created () {
    this.getProfile()
  },
  methods: {
    getProfile (curPage) {
      if (!curPage || curPage === '') {
        curPage = 1
      }
      console.log(this.formInline.userName)
      // 获取表格数据
      this.$axios.get('/users', {
        params: {
          name: this.formInline.userName,
          pageNum: curPage,
          pageSize: 10
        }
      }).then(res => {
        this.users = res.data.data.list
        // 设置分页数据
        this.setPaginations(res.data.data)
      })
    },
    handleEdit (index, row) {
      console.log(index, row)
    },
    handleDelete (index, rowId) {
      this.$axios.delete(`/users/${rowId}`).then(res => {
        if (res.data.code === '200') {
          //  注册成功
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '删除失败，请重试',
            type: 'fail'
          })
        }
        this.$router.go('/user-list')
      })
    },
    // 设置分页属性
    setPaginations (page) {
      this.pageInfo.page_index = page.pageNum
      this.pageInfo.page_size = page.pageSize
      this.pageInfo.total = page.total
      this.pageInfo.page_sizes = page.size
    }
  }
}
</script>
